<?php

    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/7/10
     * Time: 下午2:32
     */

    namespace app\payment\api;


    use app\common\controller\API;
    use app\common\utils\UtilEncryption;
    use app\member\logic\Member;
    use app\payment\model\WalletModel;

    /**
     * 钱包管理
     * Class Wallet
     *
     * @package app\payment\api
     */
    class Wallet extends API
    {

        /**
         * 我的钱包
         * api GET /payment/Wallet/get_money
         *
         * @return array data.money.title 资产名称
         * @return array data.money.value 资产值
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function get_money()
        {

            $my_property = WalletModel::userwallet(env('member_id'));
            if ($my_property['pay_password']) {
                $my_property['pay_password'] = 1;
            } else {
                $my_property['pay_password'] = 0;
            }

            $this->result($my_property);
        }

        /**
         * 获取手机验证码(验证手机号是否存在)
         * api GET /payment/wallet/get_code
         *
         * @param int phone 手机号码
         *
         * @return array data 返回值
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function get_code()
        {

            $data = $this->request->param();
            $data['member_id'] = env('member_id');;
            // 验证
            $this->check_validate($data, 'Wallet.get_code');

            $res = Member::sendPhoneCode($data);
            if ($res['type'] == 'success') {
                $this->success($res['msg']);
            } else {
                $this->error($res['msg']);
            }

        }

        /**
         * 设置交易密码
         * api post  /payment/wallet/set_pay_password
         *
         * @param int    $phone           手机号码
         * @param int    $validate_code   验证码
         * @param string $pay_password    支付密码
         * @param string $re_pay_password 确认支付密码
         */
        public function set_pay_password()
        {

            $data = $this->request->param(); //获取参数
            // 验证
            $this->check_validate($data, 'Wallet.set_pay_password');
            //            if (Cache::get('timephone_' . $data['phone']) == $data['validate_code'])
            //            {
            $reqst = WalletModel::update(['pay_password' => UtilEncryption::encryptMd5($data['pay_password'])], ['member_id' => env('member_id')]);
            if (isset($reqst)) {
                $this->success('设置成功！');
            } else {
                $this->error('设置失败！');
            }

            //            }
            //            else
            //            {
            //                $this->error('验证码不正确，或已过期，请重新获取！');
            //            }
        }

    }